<template>
  <div>
    <!-- 搜索 -->
    <el-card class="box-card">
      <el-row type="flex" align="middle">
        <el-col :span="12">
          <div class="grid-content bg-purple search">
            <el-form ref="query" :model="query" label-width="100px">
              <el-form-item style="margin-bottom:0px" label="状态">
                <el-select v-model="query.status" placeholder="请选择状态">
                  <!-- <el-option label="全部" value="0"></el-option> -->
                  <el-option label="待申请发货" value="1"></el-option>
                  <el-option label="已申请" value="2"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item style="margin-bottom:0px" label="奖品名称">
                <el-input style="width:260px" v-model="query.keywords" placeholder="请选输入奖品名称"></el-input>
              </el-form-item>
            </el-form>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple-light" style="text-align:right">
            <el-button type="primary" @click="queryAsk">查询</el-button>
            <el-button @click="queryReset">重置</el-button>
          </div>
        </el-col>
      </el-row>
    </el-card>
    <el-card class="box-card">
      <el-table
        ref="multipleTable"
        style="margin-bottom:20px"
        :header-cell-style="{background:'#eee'}"
        border
        :data="tableData"
        @selection-change="handleSelectionChange"
      >
        <el-table-column align="center" type="selection" width="55" v-if="is_show"></el-table-column>
        <el-table-column align="center" type="index" width="50" label="序号"></el-table-column>
        <el-table-column align="center" label="奖品图片">
          <template slot-scope="scope">
            <el-popover placement="top-start" title trigger="hover">
              <img :src="scope.row.img" alt style="max-width:150px" />
              <!-- <el-avatar shape="square" :size="150" fit="cover" :src="scope.row.img"></el-avatar> -->
              <el-avatar
                slot="reference"
                shape="square"
                :size="50"
                fit="scale-down"
                :src="scope.row.img"
              ></el-avatar>
              <!-- <img
                slot="reference"
                :src="scope.row.img"
                style="width: 60px;height: 60px"
              />-->
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="goods_name" label="奖品名称"></el-table-column>
        <el-table-column align="center" prop="y_price" label="成本"></el-table-column>
        <el-table-column align="center" prop="price" label="售价"></el-table-column>
        <el-table-column align="center" prop="add_time" label="获取时间">
          <template slot-scope="scope">{{returnTimeTxt(scope.row.add_time)}}</template>
        </el-table-column>
        <el-table-column align="center" prop="title" label="背包状态">
          <template slot-scope="scope">
            <div v-if="scope.row.status==1">待申请发货</div>
            <div v-if="scope.row.status==2">已申请</div>
          </template>
        </el-table-column>
      </el-table>
      <div style="text-align:right">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page="currentPage"
          :total="total"
          @current-change="currentChange"
        ></el-pagination>
      </div>
      <div style="text-align:right;margin-top:20px" v-if="is_show">
        <el-button type="primary" @click="delGive" :disabled="delIds.length==0">确定删除</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import { userBoutique } from '@/api/consumer'
import { addGive } from '@/api/marketing'
import { parseTime } from '@/utils/index'

export default {
  props: {
    user_id: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      // 分页数据
      total: 0,
      currentPage: 1,
      pageSize: 10,
      //input
      query: {
        status: '1',
        keywords: ''
      },
      tableData: [],
      delIds: [],
      is_show: true
    }
  },
  mounted() {
    // this.getUserCoupon();
  },
  watch: {
    user_id(newVal, oldVal) {
      // this.getUserBoutique();
    }
  },
  methods: {
    returnTimeTxt(e) {
      return parseTime(e, '{y}-{m}-{d} {h}:{i}:{s}')
    },
    // 获取优惠券列表
    async getUserBoutique() {
      if (this.query.status != 1) {
        this.is_show = false
      } else {
        this.is_show = true
      }
      const res = await userBoutique({
        user_id: this.user_id,
        page: this.currentPage,
        limit: this.pageSize,
        status: this.query.status,
        keywords: this.query.keywords
      })
      console.log(res)
      this.tableData = res.data
      this.total = res.count
    },
    queryAsk() {
      this.currentPage = 1
      this.getUserBoutique()
    },
    //重置
    queryReset() {
      this.resetForm('query')
      this.total = 0
      this.currentPage = 1
      this.pageSize = 10
      this.query.status = '1'
      this.query.keywords = ''
      this.getUserBoutique()
    },

    //分页改变
    currentChange(current) {
      this.currentPage = current
      this.getUserBoutique()
    },

    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleSelectionChange(val) {
      console.log(val)
      this.delIds = []
      val.forEach((item) => {
        this.delIds.push(item.id)
      })
      console.log(this.delIds)
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    async delGive() {
      const res = await addGive({
        user_id: this.user_id,
        type: '4',
        data: '',
        in_type: '',
        give_type: '',
        bag_type: '1',
        bag_id: this.delIds,
        goods_data: []
      })
      if (res.st == 1) {
        this.$message({
          message: '删除成功',
          type: 'success'
        })
        this.getUserBoutique()
      } else {
        console.log(res)
        this.$message.error('删除失败！')
      }
    }
  }
}
</script>

<style lang='scss' scoped>
.box-card {
  margin-bottom: 20px;
}
.search {
  display: flex;
  align-items: center;
}
::v-deep .el-input {
  width: 292px;
  display: block;
  position: static !important;
}
::v-deep .el-input--medium .el-input__inner {
  width: 292px;
}

::v-deep .el-form-item__label {
  padding: 0 30px 0 0;
}

::v-deep .el-form {
  display: flex;
}
</style>